home *** CD-ROM | disk | FTP | other *** search
/ Visual Cafe 3 / Visual Cafe 3.ISO / Vcafe / Main.bin / KeyEvent.java < prev    next >
Text File  |  1998-09-22  |  20KB  |  472 lines

  1. /*
  2.  * @(#)KeyEvent.java    1.26 98/07/01
  3.  *
  4.  * Copyright 1995-1998 by Sun Microsystems, Inc.,
  5.  * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
  6.  * All rights reserved.
  7.  * 
  8.  * This software is the confidential and proprietary information
  9.  * of Sun Microsystems, Inc. ("Confidential Information").  You
  10.  * shall not disclose such Confidential Information and shall use
  11.  * it only in accordance with the terms of the license agreement
  12.  * you entered into with Sun.
  13.  */
  14.  
  15. package java.awt.event;
  16.  
  17. import java.awt.Event;
  18. import java.awt.Component;
  19. import java.awt.Toolkit;
  20.  
  21. /**
  22.  * The component-level keyboard event.
  23.  *
  24.  * @version 1.23 08/04/97
  25.  * @author Carl Quinn
  26.  * @author Amy Fowler
  27.  */
  28. public class KeyEvent extends InputEvent {
  29.  
  30.     /**
  31.      * Marks the first integer id for the range of key event ids.
  32.      */
  33.     public static final int KEY_FIRST = 400;
  34.  
  35.     /**
  36.      * Marks the last integer id for the range of key event ids.
  37.      */
  38.     public static final int KEY_LAST  = 402;
  39.  
  40.     /**
  41.      * The key typed event type.  This type is generated by a combination
  42.      * of a key press followed by a key release.
  43.      */
  44.     public static final int KEY_TYPED = KEY_FIRST;
  45.  
  46.     /**
  47.      * The key pressed event type.
  48.      */
  49.     public static final int KEY_PRESSED = 1 + KEY_FIRST; //Event.KEY_PRESS
  50.  
  51.     /**
  52.      * The key released event type.
  53.      */
  54.     public static final int KEY_RELEASED = 2 + KEY_FIRST; //Event.KEY_RELEASE
  55.  
  56.     /**
  57.      * Virtual key codes.  These codes report which keyboard key has
  58.      * been pressed, rather than any character generated by one or more
  59.      * keys being pressed.  
  60.      *
  61.      * For example, pressing the Shift key will cause a KEY_PRESSED event 
  62.      * with a VK_SHIFT keyCode, while pressing the 'a' key will result in 
  63.      * a VK_A keyCode.  After the 'a' key is released, a KEY_RELEASED event 
  64.      * will be fired with VK_A, followed by a KEY_TYPED event with a keyChar 
  65.      * value of 'A'.  Key combinations which do not result in characters,
  66.      * such as action keys like F1, will not generate KEY_TYPED events.
  67.      *
  68.      * Note: not all keyboards or systems are capable of generating all
  69.      * virtual key codes.  No attempt is made in Java to artificially
  70.      * generate these keys.
  71.      *
  72.      * WARNING:  aside from those keys where are defined by the Java language
  73.      * (VK_ENTER, VK_BACK_SPACE, and VK_TAB), do not rely on the values of these
  74.      * constants.  Sun reserves the right to change these values as needed
  75.      * to accomodate a wider range of keyboards in the future.  
  76.      */
  77.     public static final int VK_ENTER          = '\n';
  78.     public static final int VK_BACK_SPACE     = '\b';
  79.     public static final int VK_TAB            = '\t';
  80.     public static final int VK_CANCEL         = 0x03;
  81.     public static final int VK_CLEAR          = 0x0C;
  82.     public static final int VK_SHIFT          = 0x10;
  83.     public static final int VK_CONTROL        = 0x11;
  84.     public static final int VK_ALT            = 0x12;
  85.     public static final int VK_PAUSE          = 0x13;
  86.     public static final int VK_CAPS_LOCK      = 0x14;
  87.     public static final int VK_ESCAPE         = 0x1B;
  88.     public static final int VK_SPACE          = 0x20;
  89.     public static final int VK_PAGE_UP        = 0x21;
  90.     public static final int VK_PAGE_DOWN      = 0x22;
  91.     public static final int VK_END            = 0x23;
  92.     public static final int VK_HOME           = 0x24;
  93.     public static final int VK_LEFT           = 0x25;
  94.     public static final int VK_UP             = 0x26;
  95.     public static final int VK_RIGHT          = 0x27;
  96.     public static final int VK_DOWN           = 0x28;
  97.     public static final int VK_COMMA          = 0x2C;
  98.     public static final int VK_PERIOD         = 0x2E;
  99.     public static final int VK_SLASH          = 0x2F;
  100.  
  101.     /** VK_0 thru VK_9 are the same as ASCII '0' thru '9' (0x30 - 0x39) */
  102.     public static final int VK_0              = 0x30;
  103.     public static final int VK_1              = 0x31;
  104.     public static final int VK_2              = 0x32;
  105.     public static final int VK_3              = 0x33;
  106.     public static final int VK_4              = 0x34;
  107.     public static final int VK_5              = 0x35;
  108.     public static final int VK_6              = 0x36;
  109.     public static final int VK_7              = 0x37;
  110.     public static final int VK_8              = 0x38;
  111.     public static final int VK_9              = 0x39;
  112.  
  113.     public static final int VK_SEMICOLON      = 0x3B;
  114.     public static final int VK_EQUALS         = 0x3D;
  115.  
  116.     /** VK_A thru VK_Z are the same as ASCII 'A' thru 'Z' (0x41 - 0x5A) */
  117.     public static final int VK_A              = 0x41;
  118.     public static final int VK_B              = 0x42;
  119.     public static final int VK_C              = 0x43;
  120.     public static final int VK_D              = 0x44;
  121.     public static final int VK_E              = 0x45;
  122.     public static final int VK_F              = 0x46;
  123.     public static final int VK_G              = 0x47;
  124.     public static final int VK_H              = 0x48;
  125.     public static final int VK_I              = 0x49;
  126.     public static final int VK_J              = 0x4A;
  127.     public static final int VK_K              = 0x4B;
  128.     public static final int VK_L              = 0x4C;
  129.     public static final int VK_M              = 0x4D;
  130.     public static final int VK_N              = 0x4E;
  131.     public static final int VK_O              = 0x4F;
  132.     public static final int VK_P              = 0x50;
  133.     public static final int VK_Q              = 0x51;
  134.     public static final int VK_R              = 0x52;
  135.     public static final int VK_S              = 0x53;
  136.     public static final int VK_T              = 0x54;
  137.     public static final int VK_U              = 0x55;
  138.     public static final int VK_V              = 0x56;
  139.     public static final int VK_W              = 0x57;
  140.     public static final int VK_X              = 0x58;
  141.     public static final int VK_Y              = 0x59;
  142.     public static final int VK_Z              = 0x5A;
  143.  
  144.     public static final int VK_OPEN_BRACKET   = 0x5B;
  145.     public static final int VK_BACK_SLASH     = 0x5C;
  146.     public static final int VK_CLOSE_BRACKET  = 0x5D;
  147.  
  148.     public static final int VK_NUMPAD0        = 0x60;
  149.     public static final int VK_NUMPAD1        = 0x61;
  150.     public static final int VK_NUMPAD2        = 0x62;
  151.     public static final int VK_NUMPAD3        = 0x63;
  152.     public static final int VK_NUMPAD4        = 0x64;
  153.     public static final int VK_NUMPAD5        = 0x65;
  154.     public static final int VK_NUMPAD6        = 0x66;
  155.     public static final int VK_NUMPAD7        = 0x67;
  156.     public static final int VK_NUMPAD8        = 0x68;
  157.     public static final int VK_NUMPAD9        = 0x69;
  158.     public static final int VK_MULTIPLY       = 0x6A;
  159.     public static final int VK_ADD            = 0x6B;
  160.     public static final int VK_SEPARATER      = 0x6C;
  161.     public static final int VK_SUBTRACT       = 0x6D;
  162.     public static final int VK_DECIMAL        = 0x6E;
  163.     public static final int VK_DIVIDE         = 0x6F;
  164.     public static final int VK_F1             = 0x70;
  165.     public static final int VK_F2             = 0x71;
  166.     public static final int VK_F3             = 0x72;
  167.     public static final int VK_F4             = 0x73;
  168.     public static final int VK_F5             = 0x74;
  169.     public static final int VK_F6             = 0x75;
  170.     public static final int VK_F7             = 0x76;
  171.     public static final int VK_F8             = 0x77;
  172.     public static final int VK_F9             = 0x78;
  173.     public static final int VK_F10            = 0x79;
  174.     public static final int VK_F11            = 0x7A;
  175.     public static final int VK_F12            = 0x7B;
  176.     public static final int VK_DELETE         = 0x7F; /* ASCII DEL */
  177.     public static final int VK_NUM_LOCK       = 0x90;
  178.     public static final int VK_SCROLL_LOCK    = 0x91;
  179.  
  180.     public static final int VK_PRINTSCREEN    = 0x9A;
  181.     public static final int VK_INSERT         = 0x9B;
  182.     public static final int VK_HELP           = 0x9C;
  183.     public static final int VK_META           = 0x9D;
  184.  
  185.     public static final int VK_BACK_QUOTE     = 0xC0;
  186.     public static final int VK_QUOTE          = 0xDE;
  187.  
  188.     /** for Asian Keyboard */
  189.     public static final int VK_FINAL          = 0x18;
  190.     public static final int VK_CONVERT        = 0x1C;
  191.     public static final int VK_NONCONVERT     = 0x1D;
  192.     public static final int VK_ACCEPT         = 0x1E;
  193.     public static final int VK_MODECHANGE     = 0x1F;
  194.     public static final int VK_KANA           = 0x15;
  195.     public static final int VK_KANJI          = 0x19;
  196.     
  197.     /**
  198.      * KEY_TYPED events do not have a defined keyCode.
  199.      */
  200.     public static final int VK_UNDEFINED      = 0x0;
  201.  
  202.     /**
  203.      * KEY_PRESSED and KEY_RELEASED events which do not map to a
  204.      * valid Unicode character do not have a defined keyChar.
  205.      */
  206.     public static final char CHAR_UNDEFINED   = 0x0;
  207.  
  208.     int  keyCode;
  209.     char keyChar;
  210.  
  211.     /*
  212.      * JDK 1.1 serialVersionUID 
  213.      */
  214.      private static final long serialVersionUID = -2352130953028126954L;
  215.  
  216.     /**
  217.      * Constructs a KeyEvent object with the specified source component,
  218.      * type, modifiers, and key.
  219.      * @param source the object where the event originated
  220.      * @id the event type
  221.      * @when the time the event occurred
  222.      * @modifiers the modifier keys down during event
  223.      * @keyCode the integer code representing the key of the event 
  224.      * @keyChar the Unicode character generated by this event, or NUL
  225.      */
  226.     public KeyEvent(Component source, int id, long when, int modifiers,
  227.                     int keyCode, char keyChar) {
  228.         super(source, id, when, modifiers);
  229.  
  230.         if (id == KEY_TYPED && keyChar == CHAR_UNDEFINED) {
  231.             throw new IllegalArgumentException("invalid keyChar");
  232.         }
  233.         if (id == KEY_TYPED && keyCode != VK_UNDEFINED) {
  234.             throw new IllegalArgumentException("invalid keyCode");
  235.         }
  236.  
  237.         this.keyCode = keyCode;
  238.         this.keyChar = keyChar;
  239.     }
  240.  
  241.     /*
  242.      * @deprecated, as of JDK1.1 - Do NOT USE; will be removed in 1.1.1.
  243.      */
  244.     public KeyEvent(Component source, int id, long when, int modifiers,
  245.                     int keyCode) {
  246.         this(source, id, when, modifiers, keyCode, (char)keyCode);
  247.     }
  248.  
  249.     /**
  250.      * Returns the integer key-code associated with the key in this event.
  251.      * For KEY_TYPED events, keyCode is VK_UNDEFINED.
  252.      */
  253.     public int getKeyCode() {
  254.         return keyCode;
  255.     }
  256.  
  257.     public void setKeyCode(int keyCode) {
  258.         this.keyCode = keyCode;
  259.     }
  260.  
  261.     public void setKeyChar(char keyChar) {
  262.         this.keyChar = keyChar;
  263.     }
  264.  
  265.     /**
  266.      * Change the modifiers for a KeyEvent.  
  267.      * <p>
  268.      * NOTE:  use of this method is not recommended, because many AWT
  269.      * implementations do not recognize modifier changes.  This is
  270.      * especially true for KEY_TYPED events where the shift modifier
  271.      * is changed.
  272.      * @deprecated, as of JDK1.1.4
  273.      */
  274.     public void setModifiers(int modifiers) {
  275.         this.modifiers = modifiers;
  276.     }
  277.  
  278.     /**
  279.      * Returns the character associated with the key in this event.
  280.      * If no valid Unicode character exists for this key event, keyChar
  281.      * is CHAR_UNDEFINED.
  282.      */
  283.     public char getKeyChar() {
  284.         return keyChar;
  285.     }
  286.  
  287.     /**
  288.      * Returns a String describing the keyCode, such as "HOME", "F1" or "A".
  289.      * These strings can be localized by changing the awt.properties file.
  290.      */
  291.     public static String getKeyText(int keyCode) {
  292.         if (keyCode >= VK_0 && keyCode <= VK_9 || 
  293.             keyCode >= VK_A && keyCode <= VK_Z) {
  294.             return String.valueOf((char)keyCode);
  295.         }
  296.  
  297.         // Check for other ASCII keyCodes.
  298.         int index = ",./;=[\\]".indexOf(keyCode);
  299.         if (index >= 0) {
  300.             return String.valueOf((char)keyCode);
  301.         }
  302.     
  303.         switch(keyCode) {
  304.           case VK_ENTER: return Toolkit.getProperty("AWT.enter", "Enter");
  305.           case VK_BACK_SPACE: return Toolkit.getProperty("AWT.backSpace", "Backspace");
  306.           case VK_TAB: return Toolkit.getProperty("AWT.tab", "Tab");
  307.           case VK_CANCEL: return Toolkit.getProperty("AWT.cancel", "Cancel");
  308.           case VK_CLEAR: return Toolkit.getProperty("AWT.clear", "Clear");
  309.           case VK_SHIFT: return Toolkit.getProperty("AWT.shift", "Shift");
  310.           case VK_CONTROL: return Toolkit.getProperty("AWT.control", "Control");
  311.           case VK_ALT: return Toolkit.getProperty("AWT.alt", "Alt");
  312.           case VK_PAUSE: return Toolkit.getProperty("AWT.pause", "Pause");
  313.           case VK_CAPS_LOCK: return Toolkit.getProperty("AWT.capsLock", "Caps Lock");
  314.           case VK_ESCAPE: return Toolkit.getProperty("AWT.escape", "Escape");
  315.           case VK_SPACE: return Toolkit.getProperty("AWT.space", "Space");
  316.           case VK_PAGE_UP: return Toolkit.getProperty("AWT.pgup", "Page Up");
  317.           case VK_PAGE_DOWN: return Toolkit.getProperty("AWT.pgdn", "Page Down");
  318.           case VK_END: return Toolkit.getProperty("AWT.end", "End");
  319.           case VK_HOME: return Toolkit.getProperty("AWT.home", "Home");
  320.           case VK_LEFT: return Toolkit.getProperty("AWT.left", "Left");
  321.           case VK_UP: return Toolkit.getProperty("AWT.up", "Up");
  322.           case VK_RIGHT: return Toolkit.getProperty("AWT.right", "Right");
  323.           case VK_DOWN: return Toolkit.getProperty("AWT.down", "Down");
  324.  
  325.           case VK_MULTIPLY: return Toolkit.getProperty("AWT.multiply", "NumPad *");
  326.           case VK_ADD: return Toolkit.getProperty("AWT.add", "NumPad +");
  327.           case VK_SEPARATER: return Toolkit.getProperty("AWT.separater", "NumPad ,");
  328.           case VK_SUBTRACT: return Toolkit.getProperty("AWT.subtract", "NumPad -");
  329.           case VK_DECIMAL: return Toolkit.getProperty("AWT.decimal", "NumPad .");
  330.           case VK_DIVIDE: return Toolkit.getProperty("AWT.divide", "NumPad /");
  331.  
  332.           case VK_F1: return Toolkit.getProperty("AWT.f1", "F1");
  333.           case VK_F2: return Toolkit.getProperty("AWT.f2", "F2");
  334.           case VK_F3: return Toolkit.getProperty("AWT.f3", "F3");
  335.           case VK_F4: return Toolkit.getProperty("AWT.f4", "F4");
  336.           case VK_F5: return Toolkit.getProperty("AWT.f5", "F5");
  337.           case VK_F6: return Toolkit.getProperty("AWT.f6", "F6");
  338.           case VK_F7: return Toolkit.getProperty("AWT.f7", "F7");
  339.           case VK_F8: return Toolkit.getProperty("AWT.f8", "F8");
  340.           case VK_F9: return Toolkit.getProperty("AWT.f9", "F9");
  341.           case VK_F10: return Toolkit.getProperty("AWT.f10", "F10");
  342.           case VK_F11: return Toolkit.getProperty("AWT.f11", "F11");
  343.           case VK_F12: return Toolkit.getProperty("AWT.f12", "F12");
  344.           case VK_DELETE: return Toolkit.getProperty("AWT.delete", "Delete");
  345.           case VK_NUM_LOCK: return Toolkit.getProperty("AWT.numLock", "Num Lock");
  346.           case VK_SCROLL_LOCK: return Toolkit.getProperty("AWT.scrollLock", "Scroll Lock");
  347.           case VK_PRINTSCREEN: return Toolkit.getProperty("AWT.printScreen", "Print Screen");
  348.           case VK_INSERT: return Toolkit.getProperty("AWT.insert", "Insert");
  349.           case VK_HELP: return Toolkit.getProperty("AWT.help", "Help");
  350.           case VK_META: return Toolkit.getProperty("AWT.meta", "Meta");
  351.           case VK_BACK_QUOTE: return Toolkit.getProperty("AWT.backQuote", "Back Quote");
  352.           case VK_QUOTE: return Toolkit.getProperty("AWT.quote", "Quote");
  353.              
  354.           case VK_FINAL: return Toolkit.getProperty("AWT.final", "Final");
  355.           case VK_CONVERT: return Toolkit.getProperty("AWT.convert", "Convert");
  356.           case VK_NONCONVERT: return Toolkit.getProperty("AWT.noconvert", "No Convert");
  357.           case VK_ACCEPT: return Toolkit.getProperty("AWT.accept", "Accept");
  358.           case VK_MODECHANGE: return Toolkit.getProperty("AWT.modechange", "Mode Change");
  359.           case VK_KANA: return Toolkit.getProperty("AWT.kana", "Kana");
  360.       case VK_KANJI: return Toolkit.getProperty("AWT.kanji", "Kanji");
  361.         }
  362.  
  363.         if (keyCode >= VK_NUMPAD0 && keyCode <= VK_NUMPAD9) {
  364.             String numpad = Toolkit.getProperty("AWT.numpad", "NumPad");
  365.         char c = (char)(keyCode - VK_NUMPAD0 + '0');
  366.             return numpad + "-" + c;
  367.         }
  368.  
  369.         String unknown = Toolkit.getProperty("AWT.unknown", "Unknown keyCode");
  370.         return unknown + ": 0x" + Integer.toString(keyCode, 16);
  371.     }
  372.  
  373.     /**
  374.      * Returns a String describing the modifier key(s), such as "Shift",
  375.      * or "Ctrl+Shift".  These strings can be localized by changing the 
  376.      * awt.properties file.
  377.      */
  378.     public static String getKeyModifiersText(int modifiers) {
  379.         StringBuffer buf = new StringBuffer();
  380.         if ((modifiers & InputEvent.META_MASK) != 0) {
  381.             buf.append(Toolkit.getProperty("AWT.meta", "Meta"));
  382.             buf.append("+");
  383.         }
  384.         if ((modifiers & InputEvent.CTRL_MASK) != 0) {
  385.             buf.append(Toolkit.getProperty("AWT.control", "Ctrl"));
  386.             buf.append("+");
  387.         }
  388.         if ((modifiers & InputEvent.ALT_MASK) != 0) {
  389.             buf.append(Toolkit.getProperty("AWT.alt", "Alt"));
  390.             buf.append("+");
  391.         }
  392.         if ((modifiers & InputEvent.SHIFT_MASK) != 0) {
  393.             buf.append(Toolkit.getProperty("AWT.shift", "Shift"));
  394.             buf.append("+");
  395.         }
  396.         if (buf.length() > 0) {
  397.             buf.setLength(buf.length()-1); // remove trailing '+'
  398.         }
  399.         return buf.toString();
  400.     }
  401.  
  402.     /** Returns whether or not the key in this event is an "action" key,
  403.      *  as defined in Event.java.
  404.      */
  405.     public boolean isActionKey() {
  406.         switch (keyCode) {
  407.           case VK_HOME:
  408.           case VK_END:
  409.           case VK_PAGE_UP:
  410.           case VK_PAGE_DOWN:
  411.           case VK_UP:
  412.           case VK_DOWN:
  413.           case VK_LEFT:
  414.           case VK_RIGHT:
  415.           case VK_F1:
  416.           case VK_F2:
  417.           case VK_F3:
  418.           case VK_F4:
  419.           case VK_F5:
  420.           case VK_F6:
  421.           case VK_F7:
  422.           case VK_F8:
  423.           case VK_F9:
  424.           case VK_F10:
  425.           case VK_F11:
  426.           case VK_F12:
  427.           case VK_PRINTSCREEN:
  428.           case VK_SCROLL_LOCK:
  429.           case VK_CAPS_LOCK:
  430.           case VK_NUM_LOCK:
  431.           case VK_PAUSE:
  432.           case VK_INSERT:
  433.               return true;
  434.         }
  435.         return false;
  436.     }
  437.  
  438.     public String paramString() {
  439.         String typeStr;
  440.         switch(id) {
  441.           case KEY_PRESSED:
  442.               typeStr = "KEY_PRESSED";
  443.               break;
  444.           case KEY_RELEASED:
  445.               typeStr = "KEY_RELEASED";
  446.               break;
  447.           case KEY_TYPED:
  448.               typeStr = "KEY_TYPED";
  449.               break;
  450.           default:
  451.               typeStr = "unknown type";
  452.         }
  453.  
  454.         String str = typeStr + ",keyCode=" + keyCode;
  455.         if (isActionKey() || keyCode == VK_ENTER || keyCode == VK_BACK_SPACE || 
  456.         keyCode == VK_TAB || keyCode == VK_ESCAPE || keyCode == VK_DELETE ||
  457.         (keyCode >= VK_NUMPAD0 && keyCode <= VK_NUMPAD9)) {
  458.             str += "," + getKeyText(keyCode);
  459.     } else if (keyChar == '\n' || keyChar == '\b' ||
  460.         keyChar == '\t' || keyChar == VK_ESCAPE || keyChar == VK_DELETE) {
  461.             str += "," + getKeyText(keyChar);
  462.         } else {
  463.             str += ",keyChar='" + keyChar + "'";
  464.         }
  465.         if (modifiers > 0) {
  466.             str += ",modifiers=" + getKeyModifiersText(modifiers);
  467.         }
  468.         return str;
  469.     }
  470.  
  471. }
  472.